﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;
using System.Data;

namespace Actuals.Data
{
    public class DalTeam : Dal<ITeam, Team>, IDalTeam
    {
        private readonly IDalManager _manager;
        private static readonly IDalTeam _instance = new DalTeam();
        private const string SP_GET_TEAMS = "usp_GetTeams";
        private const string SP_GET_TEAM = "usp_GetTeam";
        private const string SP_ADD_TEAM = "usp_AddTeam";
        private const string SP_UPDATE_TEAM = "usp_UpdateTeam";
        private const string SP_DELETE_TEAM = "usp_DeleteTeam";


        public static IDalTeam Instance
        {
            get { return _instance; }
        }
        public DalTeam()
        {
            _manager = new DalManager();
        }

        public DalTeam(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(ITeam obj, DataRow row)
        {
            obj.TeamId = _manager.GetValue<int>(row, "TeamId");
            obj.TeamDescription = _manager.GetValue<string>(row, "TeamDescription");
        }

        

        

        public IList<ITeam> GetTeams()
        {
            _manager.StoredProcedureName = SP_GET_TEAMS;
            List<ITeam> teams = new List<ITeam>();
            foreach (ITeam team in ConvertAll(_manager.ExecuteDataSet()))
                teams.Add(team);
            return teams;

        }


        public ITeam GetTeam(int teamId)
        {
            _manager.StoredProcedureName = SP_GET_TEAM;
            _manager.AddInParameter("@TeamId", DbType.Int32, teamId);
             ITeam team =Convert(_manager.ExecuteDataSet());
            return team;
        }


        public int AddTeam(ITeam team)
        {
            _manager.StoredProcedureName = SP_ADD_TEAM;
            _manager.AddInParameter("@TeamDescription", DbType.String, team.TeamDescription);
            return _manager.ExecuteNonQuery();
        }

        public int UpdateTeam(ITeam team)
        {
            _manager.StoredProcedureName = SP_UPDATE_TEAM;
            _manager.AddInParameter("@TeamId", DbType.Int32, team.TeamId);
            _manager.AddInParameter("@TeamDescription", DbType.String , team.TeamDescription);           

            return _manager.ExecuteNonQuery();

        }
        public bool DeleteTeam(int teamId)
        {

            _manager.StoredProcedureName = SP_DELETE_TEAM;
            _manager.AddInParameter("@TeamId", DbType.Int32, teamId);
            _manager.ExecuteNonQuery();
            return true;
        }
        



    }

    
}
 
       
